﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NCCITPubSaleModel;
using System.Text.RegularExpressions;

public partial class ProductInfo : PageBase
{
    private Product _CurrentProduct;

    public Product CurrentProduct
    {
        get 
        {
            if (_CurrentProduct == null)
            {
                using (NCCITPubSaleEntities ctx = new NCCITPubSaleEntities())
                {
                    int pid = Convert.ToInt32(Request["ID"]);
                    _CurrentProduct = ctx.Product.Where(p => p.ID == pid).FirstOrDefault();
                    if (_CurrentProduct == null)
                    {
                        Response.Redirect("./");
                    }
                    CurrentProduct.Image.Load();
                    CurrentProduct.CDROMTypeReference.Load();
                    CurrentProduct.VGATypeReference.Load();
                    CurrentProduct.MonitorTypeReference.Load();
                }
            }
            return _CurrentProduct;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.rptImages.DataSource = CurrentProduct.Image.ToList();
            this.rptImages.DataBind();
            this.tbMyPrice.Text = (CurrentProduct.StepPrice + CurrentProduct.CurrentPrice).ToString();
        }
    }

    public string StripHTML(string strHtml)
    {
        string strOutput = System.Web.HttpUtility.HtmlEncode(strHtml); 
        strOutput = strOutput.Replace("\r\n", "<br/>").Replace(" ", "&nbsp;");
        return strOutput;
    }

    protected void btnConfirm_Click(object sender, ImageClickEventArgs e)
    {
        if (CurrentProduct.IsFinished)
        {
            this.MPE2.InnerHtml = "对不起，此商品已经结束竞拍，手快有，手慢无啊！";
            this.ModalPopupExtender2.Show();
            return;
        }
        if (Convert.ToDouble(tbMyPrice.Text.Trim()) < (CurrentProduct.CurrentPrice + CurrentProduct.StepPrice))
        {
            this.MPE2.InnerHtml = "此商品每次至少要加价<font color='blue'>" + CurrentProduct.StepPrice.ToString(".00") + "</font>元";
            this.ModalPopupExtender2.Show();
            this.tbMyPrice.Text = (CurrentProduct.StepPrice + CurrentProduct.CurrentPrice).ToString();
            return;
        }
        else
        {
            using(NCCITPubSaleEntities ctx=new NCCITPubSaleEntities())
            {
                int pid = Convert.ToInt32(Request["ID"]);
                _CurrentProduct = ctx.Product.Where(p => p.ID == pid).FirstOrDefault();
                if (_CurrentProduct == null)
                {
                    Response.Redirect("./");
                }
                _CurrentProduct.Auction.Add(new Auction() { Account = Uid, LogTime=DateTime.Now, Price = Convert.ToDouble(tbMyPrice.Text.Trim()) });
                _CurrentProduct.CurrentPrice=Convert.ToDouble(tbMyPrice.Text.Trim());
                ctx.SaveChanges();
                _CurrentProduct = null;
                this.GridView1.DataBind();
            }
            this.tbMyPrice.Text = (CurrentProduct.StepPrice + CurrentProduct.CurrentPrice).ToString();
            this.MPE2.InnerHtml = "您已经成功出价！目前商品的价格是：￥" + CurrentProduct.CurrentPrice.ToString(".00");
            this.ModalPopupExtender2.Show();
            this.tbMyPrice.Text = (CurrentProduct.StepPrice + CurrentProduct.CurrentPrice).ToString();
        }
    }
}
